package com.mdd.system;

import cn.dev33.satoken.SaManager;

import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * 本地线程
 */
public class RtAdminThreadLocal {

    /**
     * 构造方法
     */
    public RtAdminThreadLocal() {}

    /**
     * 取得本地线程对象
     */
    private static final ThreadLocal<Map<String, Object>> MY_LOCAL = new ThreadLocal<>();

    /**
     * 写入本地线程
     */
    public static void put(String key, Object val) {
        Map<String, Object> map = MY_LOCAL.get();
        if (map == null) {
            synchronized (MY_LOCAL) {
                map = new ConcurrentSkipListMap<>();
            }
        }
        map.put(key, val);
        MY_LOCAL.set(map);
    }

    /**
     * 获取本地线程
     */
    public static Object get(String key) {
        return MY_LOCAL.get().getOrDefault(key, "");
    }

    /**
     * 获取管理员ID
     */
    public static Long getAdminId() {
        String adminId = (String) SaManager.getStpLogic("admin").getLoginId();;
        if ("".equals(adminId)) {
            return 0L;
        }
        return Long.parseLong(adminId);
    }

    /**
     * 获取角色ID
     */
    public static  Long  getRoleIds() {
        String roleIds = RtAdminThreadLocal.get("roleIds").toString();
        if ("".equals(roleIds) || "0".equals(roleIds)) {
            return 0L;
        }
        return Long.parseLong(roleIds);
    }

    /**
     * 删除本地线程
     */
    public static void remove() {
        MY_LOCAL.remove();
    }

}
